package thread;

class MyThread extends Thread {
    public MyThread(String name) {
        super(name);
    }

    public MyThread(ThreadGroup threadGroup, String name) {
        super(threadGroup, name);
    }

    @Override
    public void run() {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        int a = 5/0;
        for (int i = 0; i < 5; i++) {
            System.out.println(getName() + " 线程的i变量 " + i);
        }
    }
}

class MyExHandler implements Thread.UncaughtExceptionHandler {
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        System.out.println(t + " 线程出现了异常：" + e);
    }
}


public class ThreadGroupTest {
    public static void main(String[] args) {
        ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();
        System.out.println("主线程组的名字：" + mainGroup.getName() + "  " + mainGroup.isDaemon());
//        new MyThread("主线程组的线程").start();
        ThreadGroup tg = new ThreadGroup("新线程组");
        tg.setDaemon(true);
        System.out.println(tg.getName() + " isDaemon " + tg.isDaemon());
        MyThread myThread = new MyThread(tg, "tg组的线程甲");
        MyExHandler myExHandler = new MyExHandler();
        myThread.setUncaughtExceptionHandler(myExHandler);
        myThread.start();
        MyThread myThread2 = new MyThread(tg, "tg组的线程乙");
        myThread2.setUncaughtExceptionHandler(myExHandler);
        myThread2.start();
        System.out.println(myThread.getName() + " isDaemon " + myThread.isDaemon());
        tg.interrupt();
    }
}